<?php
include_once("_common.php");
include_once("$mw_cash[path]/cybercash.lib.php");

set_session("cash_contents", '');
set_session("cash_membership", '');
set_session("download_membership", '');

if (!$is_member)
    alert_close("로그인 후 이용하실 수 있습니다.");

if (!$bo_table || !$wr_id)
    alert_close("컨텐츠 정보가 올바르지 않습니다.");

$sql = "select * from $write_table where wr_id = '$wr_id'";
$write = sql_fetch($sql);

if (!$write)
    alert_close("컨텐츠 정보가 올바르지 않습니다.");

if (!$write[wr_contents_price])
    alert_close("무료 컨텐츠는 결제할 필요가 없습니다.");

$buy_history = false;
$con = mw_is_buy_contents($member[mb_id], $bo_table, $wr_id);
if ($con)
{
    if (!$write[wr_contents_domain]) // 도메인 입력받으면 계속 결제가능
    {
        $is_download_check = true;
        include_once("$g4[path]/skin/board/$board[bo_skin]/mw.lib/mw.skin.basic.lib.php");
        if ($mw_basic[cf_contents_shop_download_count]) {
            $sql = "select count(*) as cnt from $mw[download_log_table] where bo_table = '$bo_table' and wr_id = '$wr_id' and dl_datetime > '$con[cl_datetime]'";
            $row = sql_fetch($sql);
            if ($row[cnt] < $mw_basic[cf_contents_shop_download_count]) {
                $is_download_check = true;
            }
            else $is_download_check = false;
        }
        if ($mw_basic[cf_contents_shop_download_day] && $is_download_check) {
            $gap = floor(($g4[server_time] - strtotime($con[cl_datetime])) / (60*60*24));
            if ($gap <= $mw_basic[cf_contents_shop_download_day]) {
                $is_download_check = true;
            }
            else $is_download_check = false;
        }
        if ($is_download_check)
            alert_close("이미 결제하셨습니다.");
    }
    $buy_history = true;
}

$g4[title] = "컨텐츠 구입";
include_once("$g4[path]/head.sub.php");
?>

<style type="text/css">
body { padding:10px; }
h3 { font-size:15px; }
.contents-info { border:5px solid #efefef; padding:20px; font-weight:bold; font-size:13px; padding-left:60px; }
.contents-info td { height:25px; }
td.head { border-bottom:1px solid #ddd; padding:15px 10px 0 0; font-weight:bold; }
td.body { border-bottom:1px solid #ddd; padding:10px; }
.btn1 { background-color:#efefef; cursor:pointer; }
.hints { color:#888; margin:5px 0 5px 0; }
.blue { color:#0D6AB2; }
.button { text-align:center; margin:30px 0 30px 0; }
.btn-image { cursor:pointer; }
#payment-info { color:#888; }
</style>

<script type="text/javascript" src="<?=$mw_cash[path]?>/cybercash.js"></script>
<script type="text/javascript">
var mw_cash_path = "<?=$mw_cash[path]?>";
function form_submit(paytype) {
    fwrite.paytype.value = paytype;
    if (paytype == 'cash') {
        <? if ($write[wr_contents_price] > $mw_cash[mb_cash]) { ?>
        alert("보유하고 계신 <?=$mw_cash[cf_cash_name]?> 부족합니다.\n\n충전후 이용해주세요.");
        return false; 
        <? } ?>
        fwrite.action = "buy-contents-update.php";
    } else {
        fwrite.action = "cash-charge-update.php";
    }
    if (!confirm('정말 구입하시겠습니까?')) return false;

    fwrite.submit();
}
function account_move() {
    var msg = "";
    msg += "무통장 입금은 관리자 확인이 필요하기 때문에 바로결제가 지원되지 않습니다.\n\n";
    msg += "<?=$mw_cash[cf_cash_name]?> 충전후 다시 가입신청 해주시기 바랍니다.\n\n";
    msg += "<?=$mw_cash[cf_cash_name]?> 충전 페이지로 이동하시겠습니까?";
    if (!confirm(msg)) return;

    window.open("<?=$mw_cash[path]?>");
    self.close();
}
</script>

<h3><?=$g4[title]?></h3>

<!--<form name="fwrite" method="post" action="buy-contents-update.php" onsubmit="return form_submit()">-->
<form name="fwrite" method="post">
<input type="hidden" name="bo_table" value="<?=$bo_table?>">
<input type="hidden" name="wr_id" value="<?=$wr_id?>">
<input type="hidden" name="no" value="<?=$no?>">
<input type="hidden" name="paytype" value="">
<div class="contents-info">
    <table border="0" cellpadding="0" cellspacing="0">
    <tr><td><strong>컨텐츠 이름</strong> : <?=get_text($write[wr_subject])?></td></tr>
    <tr><td><strong>컨텐츠 가격</strong> : <span class="blue"><?=$mw_cash[cf_cash_name]?> <?=number_format($write[wr_contents_price])?> <?=$mw_cash[cf_cash_unit]?></span></td></tr>
    <tr><td><strong>나의 <?=$mw_cash[cf_cash_name]?></strong> : <?=number_format($mw_cash[mb_cash])?> <?=$mw_cash[cf_cash_unit]?></td></tr>
    <? if ($write[wr_contents_domain]) { ?>
    <tr><td><strong>구매한 이력</strong> : 
	<?
	if ($buy_history)
	    echo "<a href='$mw_cash[path]/buy-list.php?rel_table=$bo_table' target=_blank style='color:#ff0000;'>구매한 이력이 존재합니다.</a>"; 
	else 
	    echo "구매한 이력이 없습니다.";
	?> 
    </td></tr>
    <tr><td><strong>사용 도메인</strong> : <input type="text" size="30" required itemname="사용도메인" name="cl_domain" class="ed"> </td></tr>
    <? } ?>
    </tr>
    </table>
</div>

<div class="button">
<!--
<input type="submit" value="구입하기" class="btn1">
&nbsp;&nbsp;
<input type="button" value="<?=$mw_cash[cf_cash_name]?> 충전" class="btn1" onclick="window.open('<?=$mw_cash[path]?>')">
-->

<? if (1) { ?><img src="<?=$mw_cash[path]?>/img/bt_cash.gif" class="btn-image" onclick="form_submit('cash')"><? } ?>
<? if ($mw_cash[cf_card]) { ?><img src="<?=$mw_cash[path]?>/img/bt_card.gif" class="btn-image" onclick="form_submit('card')"><? } ?>
<? if ($mw_cash[cf_bank]) { ?><img src="<?=$mw_cash[path]?>/img/bt_bank.gif" class="btn-image" onclick="form_submit('bank')"><? } ?>
<? if ($mw_cash[cf_hp]) { ?><img src="<?=$mw_cash[path]?>/img/bt_hp.gif" class="btn-image" onclick="form_submit('hp')"><? } ?>
<? if ($mw_cash[cf_account]) { ?><img src="<?=$mw_cash[path]?>/img/bt_account.gif" class="btn-image" onclick="account_move()"><? } ?>
<? if ($mw_cash[cf_paypal]) { ?><img src="<?=$mw_cash[path]?>/img/bt_paypal.png" class="btn-image" onclick="form_submit('paypal')"><? } ?>
</div>


</div>
</form>

<?
include_once("$g4[path]/tail.sub.php");
